﻿using DevExpress.Utils.Win;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Popup;
using DevExpress.XtraGrid.Editors;
using DevExpress.XtraLayout;
using DXApplicationTangche.UC.圣同润.merchant;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 广州爱特安为科技.UC.圣同润.客服
{
    public partial class Form历史订单 : DevExpress.XtraEditors.XtraForm
    {
        private String brandName;
        private int brandId;
        public Form历史订单()
        {
            InitializeComponent();
            this.searchLookUpEdit品牌商.Properties.DataSource = MerchantService.getBrandAll();
            //  历史订单布局
            bandedGridView历史订单.Columns["createdate"].GroupIndex = 0;
            bandedGridView历史订单.GroupSummary.Add(DevExpress.Data.SummaryItemType.Count, "createdate");
            //bandedGridViewHistoryOrder.GroupFormat = "{1} {2}";  //默认"{0}: [#image]{1} {2}"; 字段名称：数据 计数=？
            bandedGridView历史订单.Columns["logisticsbatchno"].GroupIndex = 1;
            //bandedGridViewHistoryOrder.GroupSummary.Add(DevExpress.Data.SummaryItemType.Count, "order_sn", bandedGridViewHistoryOrder.Columns["order_sn"]);
            //bandedGridViewHistoryOrder.GroupSummary.Add(DevExpress.Data.SummaryItemType.Count, "order_sn");
            bandedGridView历史订单.GroupSummary.Add(DevExpress.Data.SummaryItemType.Sum, "total_num");
            bandedGridView历史订单.GroupFormat = "{1} {2}";  //默认"{0}: [#image]{1} {2}"; 字段名称：数据 计数=？
            bandedGridView历史订单.GroupSummary[bandedGridView历史订单.GroupSummary.Count - 1].DisplayFormat = "货品数量合计：{0:N0}";
        }


        #region 选择品牌商
        private void searchLookUpEdit品牌商_Popup(object sender, EventArgs e)
        {
            PopupSearchLookUpEditForm form = (sender as IPopupControl).PopupWindow as PopupSearchLookUpEditForm;
            SearchEditLookUpPopup popup = form.Controls.OfType<SearchEditLookUpPopup>().FirstOrDefault();
            LayoutControl layout = popup.Controls.OfType<LayoutControl>().FirstOrDefault();
            //如果窗体内空间没有确认按钮，则自定义确认simplebutton，取消simplebutton，选中结果label
            if (layout.Controls.OfType<Control>().Where(ct => ct.Name == "btOK").FirstOrDefault() == null)
            {
                //得到空的空间
                EmptySpaceItem a = layout.Items.Where(it => it.TypeName == "EmptySpaceItem").FirstOrDefault() as EmptySpaceItem;

                //得到取消按钮，重写点击事件
                Control clearBtn = layout.Controls.OfType<Control>().Where(ct => ct.Name == "btClear").FirstOrDefault();
                LayoutControlItem clearLCI = (LayoutControlItem)layout.GetItemByControl(clearBtn);
                clearBtn.Click += clear品牌商Btn_Click;

                //添加一个simplebutton控件(确认按钮)
                LayoutControlItem myLCI = (LayoutControlItem)clearLCI.Owner.CreateLayoutItem(clearLCI.Parent);
                myLCI.TextVisible = false;
            }
        }
        private void searchLookUpEdit品牌商View_Click(object sender, EventArgs e)
        {
            int[] a = this.searchLookUpEdit品牌商.Properties.View.GetSelectedRows();
            if (a.Length > 0)
            {
                foreach (int rowHandle in a)
                {
                    String id = this.searchLookUpEdit品牌商.Properties.View.GetRowCellValue(rowHandle, "id").ToString();
                    this.brandId = Convert.ToInt32(id);
                    this.brandName = this.searchLookUpEdit品牌商.Properties.View.GetRowCellValue(rowHandle, "name").ToString();
                    //  下拉选品牌商，查询历史订单
                    this.gridControl历史订单.DataSource = MerchantService.getHistoryOrder(this.brandId);
                    this.bandedGridView历史订单.RefreshData();
                    bandedGridView历史订单.OptionsBehavior.AutoExpandAllGroups = true;
                }
            }
        }

        /// <summary>
        /// 清除按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void clear品牌商Btn_Click(object sender, EventArgs e)
        {
            this.searchLookUpEdit品牌商.ToolTip = null;
            searchLookUpEdit品牌商.EditValue = null;
        }
        private void searchLookUpEdit品牌商_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            if (null != e.Value)
                e.DisplayText = this.brandName;
        }
        #endregion

    }
}